<?php
/**
 * User: onwer
 * Date: 12-4-7
 * Time: 上午12:34
 */
class FileManage
{
    public $fileroot=SITEABSDIR;
    public $curdir=array();
    public $filter=array('.','..','.idea','.svn','admin','config');
    public function isfile($curdir,$name){
        return is_file($this->fileroot.$curdir.$name);
    }
    public function isdir($curdir,$name){
        return is_dir($this->fileroot.$curdir.$name);
    }
    public function setcurarr($curdir){
        //$tmparr=explode('/',$curdir);
        preg_match_all('/[a-z]*\//i',$curdir,$matchs);
        $parentdir='';
        $tmparr=array();
        foreach ($matchs[0] as $dir) {
            array_push($tmparr,array('name'=>$dir,'parent'=>$parentdir));
            $parentdir.=$dir;
        }

        $this->curdir=$tmparr;
    }
    public function open($filename){
        return file_get_contents($this->fileroot.$filename);
    }
    public function write($filename,$data){
        file_put_contents($this->fileroot.$filename,$data);
        return true;
    }
    public function getCur($curdir='/'){

        $this->setcurarr($curdir);
        $abccurdir=$this->fileroot.$curdir;

        $filere=array();
        $dirre=array();
        if ($handle = opendir($abccurdir)) {

            while (false !== ($filename = readdir($handle))) {
                if(in_array($filename,$this->filter)){
                    continue;
                }
                $absname=$abccurdir.$filename;
                if(is_dir($absname)){
                    array_push($dirre,array('type'=>'目录','name'=>$filename.'/','mtime'=>date ("Y-m-d H:i:s", filemtime($absname))));
                }
                if(is_file($absname)){
                    array_push($filere,array('type'=>'文件','name'=>$filename,'mtime'=>date ("Y-m-d H:i:s", filemtime($absname))));
                }
            }


            closedir($handle);
        }


         return array_merge($dirre,$filere);

    }
}
